package com.jianyun.jyzs.model;

import android.util.Log;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.CrmCalendarCountBean;
import com.jianyun.jyzs.bean.CrmCalendarListBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.ICrmCaledarModel;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/12/12.
 */

public class CrmCalendarModel implements ICrmCaledarModel {

    private static CrmCalendarModel model;

    private CrmCalendarModel() {
    }

    public static CrmCalendarModel getInstance() {
        if (null == model) {
            model = new CrmCalendarModel();
        }
        return model;
    }


    /**
     * 获取当月预约次数
     *
     * @param enterpriseCode
     * @param userId
     * @param month
     * @param type
     * @param onGetMonthCountLisenter
     */
    @Override
    public void getCrmMonthCount(String enterpriseCode, String userId, String type, String year_month,
                                 final OnGetMonthCountLisenter onGetMonthCountLisenter) {

        Api api = RetrofitHelper.builderErpRoot().create(Api.class);

        api.getCrmMonthCount("GetCrmCount", enterpriseCode, userId, type, year_month,
                "jyzs", "Android", "1.0.2")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        try {
                            CrmCalendarCountBean crmCalendarCountBean = gson.fromJson(s, CrmCalendarCountBean.class);
                            boolean result = crmCalendarCountBean.getResult();
                            onGetMonthCountLisenter.onSuccess(crmCalendarCountBean.getData());
                        } catch (Exception e) {
                            e.printStackTrace();
                            onGetMonthCountLisenter.onFailed();

                        }
                        //Log.i("test", "获取预约次数：" + o.toString());
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("err", "查询预约次数失败");
                        e.printStackTrace();
                        onGetMonthCountLisenter.onFailed();
                    }
                });
    }

    @Override
    public void getCrmDateList(String enterpriseCode, String userId, String type,
                               String workdate, final OnGetCrmDateLisenter onGetCrmDateLisenter) {

        Api api = RetrofitHelper.builderErpRoot().create(Api.class);
        api.quearyCrmDateList("GetCrmInforByWorkDate", enterpriseCode, userId, type, workdate,
                "jyzs", "Android", "1.0.2")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        try {
                            String json = gson.toJson(o);
                            CrmCalendarListBean crmCalendarListBean = gson.fromJson(json, CrmCalendarListBean.class);
                            onGetCrmDateLisenter.onSuccess(crmCalendarListBean.getData());
                        } catch (Exception e) {
                            e.printStackTrace();
                            onGetCrmDateLisenter.onFailed();
                        }

                        // Log.i("test", "获取单日预约详情：" + o.toString());
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("err", "查询单日预约详情失败");
                        e.printStackTrace();
                        onGetCrmDateLisenter.onFailed();
                    }
                });

    }
}
